/**
 * 分享红包规则
 * 
 * @return {}
 */
var ruleGroupGrid = function() {
	var column = new Ext.grid.ColumnModel({
		columns : [new Ext.grid.RowNumberer({
							width : 35
						}), {
					dataIndex : "id",
					header : '规则组编号',
					width : 60
				}, {
					header : '规则名称',
					dataIndex : "ruleGroupName",
					width : 80,
					renderer : function(value, meta, record) {
						if (value) {
							meta.attr = 'ext:qtitle="规则名称:" ext:qtip="' + value
									+ '"';
							return value;
						}
					}
				}, {
					header : "是否可用",
					dataIndex : "isEnable",
					width : 60,
					renderer : function(val) {
						return PmsUtil.FixingIsEnable(val);
					}
				}, {
					header : '创建时间',
					width : 130,
					dataIndex : 'createdDate'
				}, {
					header : "创建人",
					dataIndex : "createdName",
					width : 60
				}, {
					header : "管理",
					width : 120,
					dataIndex : "id",
					sortable : false,
					renderer : function(j, i, g, m, h) {
						var l = g.data.id;
						// var isEnable = g.data.isEnable;
						var k = "";
						if (isGranted("ruleGroupManager_编辑")) {
							k += '&nbsp;&nbsp;<a href="#" onclick="updateRuleGroup('
									+ l + ');" >编辑</a>';
						}
						
						if (isGranted("ruleGroupManager_新增规则")) {
							k += '&nbsp;&nbsp;<a href="#" onclick="addSalesRule('
									+ l + ');" >添加规则</a>';
						}
						return k;
					}
				}],
		defaults : {
			remoteSort : false,
			menuDisabled : false,
			width : 100
		}
	});

	var ruleGroupStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							method : 'POST',
							url : __ctxPath
									+ "/couponRuleManager/selectRuleGroup"
						}),
				remoteSort : false,
				baseParams : {
					start : 0,
					limit : 25
				},
				reader : new Ext.data.JsonReader({
							totalProperty : "totalCount",
							root : "list",
							fields : [{
										name : "id"
									}, {
										name : "ruleGroupName"
									}, {
										name : "isEnable"
									}, {
										name : "createdDate"
									}, {
										name : "createdName"
									}]

						})
			});

	// 定义grid的toolbar
	var ruleGroupTb = new Ext.Toolbar({
				id : "ruleGroupTb",
				height : 25,
				items : []
			});
	ruleGroupTb.add(new Ext.scm.Button({
				text : "添加规则组",
				width : 80,
				iconCls : "btn-add",
				hsValue : 'ruleGroupManager_添加规则组',
				pressed : true,
				listeners : {
					"click" : function() {
						addRuleGroup();
					}
				}
			}));
	ruleGroupTb.add(new Ext.Toolbar.Spacer());
	ruleGroupTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				hsValue:'ruleGroupManager_刷新',
				width : 80,
				pressed : true,
				handler : function() {
					queryRuleGroup();
				}
			}));

	var ruleGroupGrid = new Ext.grid.GridPanel({
				id : 'ruleGroupGrid',
				title : '规则组信息',
				anchor : '100% 100%',
				store : ruleGroupStore,
				stripeRows : true,
				columnLines : true,
				clicksToEdit : 1,
				cm : column,
				loadMask : true,
				tbar : ruleGroupTb,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					forceFit : true,
					autoFill : true,
					emptyText : '没有您想要的数据'
				},
				bbar : new PG.PagingBar({
							id : 'ruleGroupPage',
							store : ruleGroupStore
						})
			});
	return ruleGroupGrid;
};

// 查询
function queryRuleGroup() {
	var grid = Ext.getCmp("ruleGroupGrid");
	var store = grid.store;
	var pagesize = Ext.getCmp('ruleGroupPage').pageSize;
	store.load({
				params : {
					start : 0,
					limit : pagesize
				}
			});
}

// 添加
function addRuleGroup() {
	try {
		addOrUpdRuleGroupWin(null, "添加规则组").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/couponRule/ruleGroupForm.js'],
				function() {
					addOrUpdRuleGroupWin(null, "添加规则组").show();
				});
	}
}

// 修改
function updateRuleGroup(id) {
	try {
		addOrUpdRuleGroupWin(id, "编辑规则组").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/couponRule/ruleGroupForm.js'],
				function() {
					addOrUpdRuleGroupWin(id, "编辑规则组").show();
				});
	}
}

// 添加规则
function addSalesRule(id){
	try {
		addSalesRuleWin(id, "添加规则").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/couponRule/addSalesRuleForm.js'],
				function() {
					addSalesRuleWin(id, "添加规则").show();
				});
	}
}

Ext.getCmp("centerTabPanel").getActiveTab().add(ruleGroupGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
